home *** CD-ROM | disk | FTP | other *** search
/ Atari Mega Archive 1 / Atari Mega Archive - Volume 1.iso / telecomm / sticpsrc.lzh / SOURCE.ARC / NRSUBR.C < prev    next >
C/C++ Source or Header  |  1990-04-08  |  6KB  |  291 lines

  1. #include "global.h"
  2. #include "mbuf.h"
  3. #include "timer.h"
  4. #include "ax25.h"
  5. #include "netrom.h"
  6. #include "lapb.h"
  7. #include <ctype.h>
  8.  
  9. /* Functions for level 3 and 4 net/rom support */
  10.  
  11. /* Convert a net/rom network header to host format structure
  12.  * Return -1 if error, 0 if OK
  13.  */
  14.  
  15. int
  16. ntohnr3(hdr,bpp)
  17. register struct nr3hdr *hdr;    /* output structure */
  18. struct mbuf **bpp;
  19. {
  20.     char *getaxaddr();
  21.     char buf[AXALEN];
  22.     unsigned char ttl;
  23.  
  24.     if (pullup(bpp,buf,AXALEN) < AXALEN)
  25.         return -1;
  26.     getaxaddr(&hdr->source,buf);
  27.  
  28.     if (pullup(bpp,buf,AXALEN) < AXALEN)
  29.         return -1;
  30.     getaxaddr(&hdr->dest,buf);
  31.  
  32.     if (pullup(bpp,&ttl,1) != 1)
  33.         return -1;
  34.     hdr->ttl = ttl;
  35.  
  36.     return 0;
  37. }
  38.  
  39. /* Convert a host-format net/rom level 3 header into an mbuf ready
  40.  * for transmission.
  41.  */
  42.  
  43. struct mbuf *
  44. htonnr3(hdr)
  45. register struct nr3hdr *hdr;
  46. {
  47.     struct mbuf *rbuf;
  48.     register char *cp;
  49.     char *putaxaddr();
  50.  
  51. #ifdef DEBUG
  52.     if (hdr == (struct nr3hdr *) NULL)
  53.         return NULLBUF;
  54. #endif
  55.  
  56.     /* Allocate space for return buffer */
  57.     if ((rbuf = alloc_mbuf(NR3HLEN)) == NULLBUF)
  58.         return NULLBUF;
  59.  
  60.     rbuf->cnt = NR3HLEN;
  61.  
  62.     /* Now convert */
  63.     cp = rbuf->data;
  64.  
  65.     hdr->source.ssid &= ~E;        /* source E-bit is always off */
  66.     hdr->dest.ssid |= E;        /* destination E-bit always set */
  67.  
  68.     cp = putaxaddr(cp,&hdr->source);
  69.     cp = putaxaddr(cp,&hdr->dest);
  70.     *cp = hdr->ttl;
  71.  
  72.     return rbuf;
  73. }
  74.  
  75. /* Convert a net/rom routing broadcast destination subpacket from
  76.  * network format to a host format structure.  Return -1 if error,
  77.  * 0 if OK.
  78.  */
  79. int ntohnrdest(ds,bpp)
  80. register struct nr3dest *ds;
  81. struct mbuf **bpp;
  82. {
  83.     char buf[AXALEN];
  84.     char quality;
  85.  
  86.     /* get destination callsign */
  87.     if (pullup(bpp,buf,AXALEN) < AXALEN)
  88.         return -1;
  89.     memcpy(ds->dest.call,buf,ALEN);
  90.     ds->dest.ssid = buf[ALEN];
  91.  
  92.     /* get destination alias */
  93.     if (pullup(bpp,ds->alias,ALEN) < ALEN)
  94.         return -1;
  95.     ds->alias[ALEN] = '\0';
  96.  
  97.     /* get best neighbor callsign */
  98.     if (pullup(bpp,buf,AXALEN) < AXALEN)
  99.         return -1;
  100.     memcpy(ds->neighbor.call,buf,ALEN);
  101.     ds->neighbor.ssid = buf[ALEN];
  102.  
  103.     /* get route quality */
  104.     if (pullup(bpp,&quality,1) < 1)
  105.         return -1;
  106.     ds->quality = uchar(quality);
  107.  
  108.     return 0;
  109. }
  110.  
  111. /* Transfer a host-format net/rom destination subpacket into a
  112.  * buffer ready for transmission as part of a route broadcast
  113.  * packet. Returns next value of buffer pointer.
  114.  */
  115. char *
  116. htonnrdest(cp,ds)
  117. register char *cp;
  118. register struct nr3dest *ds;
  119. {
  120. #ifdef DEBUG
  121.     if (ds == (struct nr3dest *) NULL)
  122.         return NULLCHAR;
  123. #endif
  124.  
  125.     memcpy(cp,ds->dest.call,ALEN);
  126.     cp += ALEN;
  127.     *cp++ = ds->dest.ssid;
  128.  
  129.     memcpy(cp,ds->alias,ALEN);
  130.     cp += ALEN;
  131.  
  132.     memcpy(cp,ds->neighbor.call,ALEN);
  133.     cp += ALEN;
  134.     *cp++ = ds->neighbor.ssid;
  135.  
  136.     *cp++ = uchar(ds->quality);
  137.  
  138.     return cp;
  139. }
  140.  
  141. /* Convert a net/rom transport header to host format structure
  142.  * Return -1 if error, 0 if OK
  143.  */
  144.  
  145. int
  146. ntohnr4(hdr,bpp)
  147. register struct nr4hdr *hdr;    /* output structure */
  148. struct mbuf **bpp;
  149. {
  150.     unsigned char buf[max(NR4HLEN,AXALEN)];
  151.  
  152.     if (pullup(bpp,buf,NR4HLEN) < NR4HLEN)
  153.         return -1;
  154.  
  155.     memset(hdr,0,sizeof(struct nr4hdr));
  156.     hdr->opcode = buf[4];
  157.  
  158.     switch (buf[4] & NR4OMASK) {    /* look at opcode byte, lower 4 bits */
  159.     case NR4PID:            /* network protocol extension */
  160.         hdr->family = buf[0];
  161.         hdr->proto = buf[1];
  162.         break;
  163.  
  164.     case NR4CONRQ:            /* connect request */
  165.         hdr->my.index = buf[0];
  166.         hdr->my.id = buf[1];
  167.  
  168.         if (pullup(bpp,buf,1) != 1)
  169.             return -1;
  170.         hdr->window = buf[0];
  171.  
  172.         if (pullup(bpp,buf,AXALEN) < AXALEN)
  173.             return -1;
  174.         getaxaddr(&hdr->suser,buf);
  175.  
  176.         if (pullup(bpp,buf,AXALEN) < AXALEN)
  177.             return -1;
  178.         getaxaddr(&hdr->snode,buf);
  179.         break;
  180.  
  181.     case NR4CONACK:            /* connect acknowledge */
  182.         hdr->your.index = buf[0];
  183.         hdr->your.id = buf[1];
  184.         hdr->my.index = buf[2];
  185.         hdr->my.id = buf[3];
  186.         if (pullup(bpp,buf,1) != 1)
  187.             return -1;
  188.         hdr->window = buf[0];
  189.         break;
  190.  
  191.     case NR4DISRQ:            /* disconnect request */
  192.     case NR4DISACK:            /* disconnect acknowledge */
  193.         hdr->your.index = buf[0];
  194.         hdr->your.id = buf[1];
  195.         break;
  196.  
  197.     case NR4INFO:            /* information */
  198.         hdr->your.index = buf[0];
  199.         hdr->your.id = buf[1];
  200.         hdr->txseq = buf[2];
  201.         hdr->rxseq = buf[3];
  202.         break;
  203.  
  204.     case NR4INFACK:            /* information acknowledge */
  205.         hdr->your.index = buf[0];
  206.         hdr->your.id = buf[1];
  207.         hdr->rxseq = buf[3];
  208.         break;
  209.  
  210.     default:            /* unknown opcode */
  211.         return -1;
  212.     }
  213.     return 0;
  214. }
  215.  
  216. /* Convert a host-format net/rom level 4 header into an mbuf ready
  217.  * for transmission.
  218.  */
  219.  
  220. struct mbuf *
  221. htonnr4(hdr)
  222. register struct nr4hdr *hdr;
  223. {
  224.     register struct mbuf *rbuf;
  225.  
  226. #ifdef DEBUG
  227.     if (hdr == (struct nr4hdr *) NULL)
  228.         return NULLBUF;
  229. #endif
  230.  
  231.     /* Allocate space for return buffer */
  232.     /* (one extra for CONRQ/CONACK plus 14 more for CONRQ) */
  233.  
  234.     if ((rbuf = alloc_mbuf(NR4HLEN + 1 +
  235.                    ((hdr->opcode == NR4CONRQ)? 14:0))) == NULLBUF)
  236.         return NULLBUF;
  237.  
  238.     rbuf->cnt = NR4HLEN;
  239.     memset(rbuf->data,0,NR4HLEN - 1);
  240.     rbuf->data[4] = hdr->opcode;
  241.  
  242.     switch (rbuf->data[4] & NR4OMASK) {    /* look at opcode byte, lower 4 bits */
  243.     case NR4PID:            /* network protocol extension */
  244.         rbuf->data[0] = hdr->family;
  245.         rbuf->data[1] = hdr->proto;
  246.         break;
  247.  
  248.     case NR4CONRQ:            /* connect request */
  249.         rbuf->data[0] = hdr->my.index;
  250.         rbuf->data[1] = hdr->my.id;
  251.         rbuf->data[5] = hdr->window;
  252.         putaxaddr(&rbuf->data[6],&hdr->suser);
  253.         putaxaddr(&rbuf->data[13],&hdr->snode);
  254.         rbuf->cnt += 15;
  255.         break;
  256.  
  257.     case NR4CONACK:            /* connect acknowledge */
  258.         rbuf->data[0] = hdr->your.index;
  259.         rbuf->data[1] = hdr->your.id;
  260.         rbuf->data[2] = hdr->my.index;
  261.         rbuf->data[3] = hdr->my.id;
  262.         rbuf->data[5] = hdr->window;
  263.         rbuf->cnt++;
  264.         break;
  265.  
  266.     case NR4DISRQ:            /* disconnect request */
  267.     case NR4DISACK:        /* disconnect acknowledge */
  268.         rbuf->data[0] = hdr->your.index;
  269.         rbuf->data[1] = hdr->your.id;
  270.         break;
  271.  
  272.     case NR4INFO:            /* information */
  273.         rbuf->data[0] = hdr->your.index;
  274.         rbuf->data[1] = hdr->your.id;
  275.         rbuf->data[2] = hdr->txseq;
  276.         rbuf->data[3] = hdr->rxseq;
  277.         break;
  278.  
  279.     case NR4INFACK:            /* information acknowledge */
  280.         rbuf->data[0] = hdr->your.index;
  281.         rbuf->data[1] = hdr->your.id;
  282.         rbuf->data[3] = hdr->rxseq;
  283.         break;
  284.  
  285.     default:            /* unknown opcode */
  286.         return free_p(rbuf);
  287.     }
  288.  
  289.     return rbuf;
  290. }
  291.